package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.DrainageTubeRecordDao;
import com.jyxd.web.data.basic.DrainageTubeRecord;
import com.jyxd.web.data.dto.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class DrainageTubeRecordService {

    @Autowired
    private DrainageTubeRecordDao drainageTubeRecordDao;

    /**
     * 新增一条插拔管记录主表记录
     *
     * @param drainageTubeRecord
     * @return boolean
     */
    public boolean insert (DrainageTubeRecord drainageTubeRecord) {
        return drainageTubeRecordDao.insert(drainageTubeRecord);
    }

    /**
     * 根据主键id查询一条插拔管记录主表记录
     *
     * @param id
     * @return Handover
     */
    public DrainageTubeRecord queryData (String id) {
        return drainageTubeRecordDao.queryData(id);
    }

    /**
     * 更新一条插拔管记录主表记录
     *
     * @param drainageTubeRecord
     * @return boolean
     */
    public HttpResult update (DrainageTubeRecord drainageTubeRecord) {
        if (Objects.nonNull(drainageTubeRecord.getId())) {
            drainageTubeRecordDao.update(drainageTubeRecord);
            return new HttpResult(200, "数据更新成功");
        }
        return new HttpResult(200, "数据更新失败");
    }

    /**
     * 根据条件分页查询插拔管记录主表记录列表
     *
     * @param map
     * @return list
     */
    public HttpResult queryList (Map<String, Object> map) {
        Integer totalCount = null;
        if (! CollectionUtils.isEmpty(map) && map.containsKey("start")) {
            totalCount = drainageTubeRecordDao.queryNum(map);
            map.put("start", ((int) map.get("start") - 1) * (int) map.get("size"));
        }
        List<DrainageTubeRecord> drainageTubeRecordList = drainageTubeRecordDao.queryList(map);
        if (CollectionUtils.isEmpty(drainageTubeRecordList)) {
            return new HttpResult(200, "暂无数据");
        }
        return new HttpResult(200, drainageTubeRecordList, totalCount, "查询成功");
    }


    /**
     * 根据条件查询列表总记录数
     *
     * @param map
     * @return
     */
    public int queryNum (Map<String, Object> map) {
        return drainageTubeRecordDao.queryNum(map);
    }

    public HttpResult deleteData (Map<String, Object> map) {
        if (CollectionUtils.isEmpty(map)) {
            return new HttpResult(200, "参数不存在，删除错误");
        }
        if (! map.containsKey("id")) {
            return new HttpResult(200, "数据id未传，删除错误");
        }
        DrainageTubeRecord drainageTubeRecord = drainageTubeRecordDao.queryData(String.valueOf(map.get("id")));
        if (Objects.isNull(drainageTubeRecord)) {
            return new HttpResult(200, "数据不存在");
        }
        drainageTubeRecord.setStatus(-1);
        drainageTubeRecordDao.update(drainageTubeRecord);
        return new HttpResult(200, "删除成功");
    }
}
